You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
perf(paint): share static font instances across renders - #444
paint.Fonts.glyphFont built a static instance of a variable font
(opentype.(*Font).Instance) into a cache that lives on the per-render Fonts.
A vector export creates one Fonts per page (vectorpage.go), so every page
rebuilt every instance.
The instance cache is now process-wide (paint/sharedinstances.go), the
companion of the shared parsed fonts from PR 291:
key: (parsed font identity, weight already clamped to the font's own axis);
bound: 64 entries, oldest evicted first; a failed build is not kept (the next
caller retries, and the page still draws the default master as before);
concurrent pages: a per-entry sync.Once makes them wait for one build; the
map is behind a mutex; Fonts.instances stays as a lock-free per-render view.
Output is unchanged: the same instance bytes, the same fallback.
Tests (go test -race ./paint . green): TestNewFontsShareInstances (two NewFonts build once, counted through the instantiate seam, clamped weights
share), TestSharedInstancesConcurrent (16 parallel pages, one build), TestSharedInstancesBounded, and the existing TestGlyphFontInstance.
BenchmarkGlyphFontPages: a cold 20-page export, one NewFonts per page, 4
weights of a variable font each (Outfit-wght); -count=5, M4 Max, allocation
counts are the stable column.
allocs/op
B/op
ns/op (indicative)
before (v0.47.2+13)
2,265,405
412,514,268
~102 ms
after
113,716
20,676,888
~5.2 ms
(Before, each page re-instanced; after, only the first page does: 20x fewer
allocations.) README updated.
Downstream measurement: go-slides v0.26.0 with this branch (1aa085b) swapped in through a replace, against v0.26.0 as released (engine v0.47.2). slidesbench -decks generated-100 -modes pdf,pptx -cores 1,all -rounds 5, on an Apple M4 Max with 16 CPUs, under macOS 27.2.1, at a 1-minute load of 6.0 when the run started. Each figure is the median of 5 rounds after a discarded warm-up; the range in brackets is the minimum and maximum. Peak memory is the resident size of the whole process tree.
Mode
Tool
Wall (ms)
Peak (MiB)
Output
pdf
v0.26.0, 16 cores (engine v0.47.2)
2692 [2654–2738]
2183
1005.5 KiB
pdf
this branch, 1 core
557 [545–575]
177
792.4 KiB
pdf
this branch, 16 cores
260 [254–261]
171
792.4 KiB
pdf
marp-cli 4.5.1
1549 [1538–1563]
1183
835.8 KiB
pptx
v0.26.0, 16 cores (engine v0.47.2)
2640 [2609–2742]
2003
995.6 KiB
pptx
this branch, 1 core
710 [689–776]
176
995.6 KiB
pptx
this branch, 16 cores
287 [283–320]
194
995.6 KiB
pptx
marp-cli 4.5.1
7281 [7252–7841]
1053
7.2 MiB
This undoes the regression bisected in go-slides/slides#117. Peak memory goes from about 2.1 GiB to 177 MiB, back to the level before engine v0.45.10. On one core, the PDF is now about 2.8× faster than marp-cli.
The PDF is smaller because each page used to embed its own instance of the font. pdfops objects counts 102 FontFile entries in the v0.47.2 PDF and 3 with this branch. I drew all 100 pages of both PDFs at 72 dpi with pdfconv from-pdf and compared them with pngeq: all 100 are pixel-identical, with a largest channel difference of 0.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #443
Refs go-slides/slides#117
paint.Fonts.glyphFontbuilt a static instance of a variable font(
opentype.(*Font).Instance) into a cache that lives on the per-renderFonts.A vector export creates one
Fontsper page (vectorpage.go), so every pagerebuilt every instance.
The instance cache is now process-wide (
paint/sharedinstances.go), thecompanion of the shared parsed fonts from PR 291:
caller retries, and the page still draws the default master as before);
sync.Oncemakes them wait for one build; themap is behind a mutex;
Fonts.instancesstays as a lock-free per-render view.Output is unchanged: the same instance bytes, the same fallback.
Tests (
go test -race ./paint .green):TestNewFontsShareInstances(twoNewFontsbuild once, counted through theinstantiateseam, clamped weightsshare),
TestSharedInstancesConcurrent(16 parallel pages, one build),TestSharedInstancesBounded, and the existingTestGlyphFontInstance.BenchmarkGlyphFontPages: a cold 20-page export, oneNewFontsper page, 4weights of a variable font each (Outfit-wght);
-count=5, M4 Max, allocationcounts are the stable column.
(Before, each page re-instanced; after, only the first page does: 20x fewer
allocations.) README updated.
🤖 Generated with Claude Code